/**
 *
 */
package cn.rengy.tpa.wxmp.user;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import org.apache.http.Consts;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;

import cn.rengy.tool.core.util.ExceptionPrintUtils;
import cn.rengy.tool.http.HttpUtils;
import cn.rengy.tpa.wxmp.exception.WeChatException;
import cn.rengy.tpa.wxmp.token.AccessToken;
import cn.rengy.tpa.wxmp.util.WeChatUtil;


public class AccountManager {
    private static Logger logger = LoggerFactory.getLogger(AccountManager.class);

    //长链接转短链接接口
    private static final String SHORTURL_POST_URL = "https://api.weixin.qq.com/cgi-bin/shorturl?access_token=";
    //生成带参数的二维码
    private static final String QRCODE_CREATE_URL = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=";
    private static final String SHOWQRCODE_URL = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=";
    static enum QrcodeType {
    	/** 整型临时二维码 */
    	QR_SCENE,
    	/** 字符串形式临时二维码 */
    	QR_STR_SCENE,
    	/** 整型永久二维码 */
    	QR_LIMIT_SCENE,
    	/** 字符串形式 永久二维码*/
    	QR_LIMIT_STR_SCENE;
    }
    
    /**
     * 长链接转短链接接口
     *
     * @param longUrl 需要转换的长链接
     * @return
     */
    public static String shortUrl(AccessToken accessToken,String longUrl) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("action", "long2short");
        jsonObject.put("long_url", longUrl);
        String requestData = jsonObject.toString();
        logger.info("request data " + requestData);
        String resultStr = HttpUtils.post(SHORTURL_POST_URL + accessToken.token(), requestData);
        logger.info("return data " + resultStr);
        try {
            WeChatUtil.isSuccess(resultStr);
        } catch (WeChatException e) {
            logger.error(e.getMessage());
            logger.error(ExceptionPrintUtils.getTrace(e));
            return null;
        }
        JSONObject resultJson = JSONObject.parseObject(resultStr);
        return resultJson.getString("short_url");
    }

    

    /**
     * 创建永久二维码
     *
     * @param sceneStr 场景值ID,长度限制为1到64
     * @return
     */
    public static Qrcode createQrcodePerpetual(AccessToken accessToken,String scene_str) {
    	//{"action_name": "QR_LIMIT_STR_SCENE", "action_info": {"scene": {"scene_str": "test"}}}
    	JSONObject ticketJson = new JSONObject();
        ticketJson.put("action_name", QrcodeType.QR_LIMIT_STR_SCENE.name());
        JSONObject sceneJson = new JSONObject();
        sceneJson.put("scene_str", scene_str);
        JSONObject actionInfoJson = new JSONObject();
        actionInfoJson.put("scene", sceneJson);
        ticketJson.put("action_info", actionInfoJson);
        String postData = ticketJson.toString();
        JSONObject resultObject=WeChatUtil.postSuccess(QRCODE_CREATE_URL, postData, accessToken);
        logger.debug("创建永久二维码result:{}",resultObject.toString());
        Qrcode qrcode=new Qrcode();
        String ticket=resultObject.getString("ticket");
        qrcode.setTicket(ticket);
        String url=resultObject.getString("url");
        qrcode.setUrl(url);
        qrcode.setShow_url(SHOWQRCODE_URL + urlEncode(ticket));
        qrcode.setScene_str(scene_str);
        return qrcode;
    }
    private static String urlEncode(String str) {
        String result = str;
        try {
        	if(str!=null){
        		result = URLEncoder.encode(str, Consts.UTF_8.name());
        	}
        } catch (UnsupportedEncodingException e) {
        }
        return result;
    }
    /**
     * 
     * @param accessToken
     * @param scene_str
     * @param expireSeconds
     * @return
     */
    public static Qrcode createQrcodeTemporary(AccessToken accessToken,String scene_str, int expireSeconds) {
        return null;
    }


}
